package personnel.department.dao;
import java.util.List;

import org.hibernate.Session;

import personnel.department.dao.entity.UniqueObject;

import com.google.common.base.Preconditions;

public class DAOImpl implements DAO {

	@Override
	public void add(UniqueObject object) {
		Preconditions.checkNotNull(object);
		Session session = HibernateUtils.getSession();
			session.beginTransaction();
			session.saveOrUpdate(object);
			session.getTransaction().commit();
	}

	@Override
	public UniqueObject getObjectById(Long id, Class<? extends UniqueObject> type) {
		Preconditions.checkNotNull(id);
		Preconditions.checkNotNull(id);
		Session session = HibernateUtils.getSession();
		UniqueObject object = (UniqueObject) session.load(type, id);
		return object;
	}

	@Override
	public List<UniqueObject> getAllObjects(Class<? extends UniqueObject> type) {
		Preconditions.checkNotNull(type);
		Session session = HibernateUtils.getSession();
		List<UniqueObject> result = session.createCriteria(type).list();
		return result;
	}

	@Override
	public void updateObject(UniqueObject object) {
		Preconditions.checkNotNull(object);
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.update(object);
		session.getTransaction().commit();
	}

	@Override
	public void deleteObject(UniqueObject object) {
		Preconditions.checkNotNull(object);
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.delete(object);
		session.getTransaction().commit();
		
	}

	@Override
	public void addAll(List<UniqueObject> objects) {
		objects.stream().forEach((object) -> add(object));
	}

}
